class Solution {
public:
    ListNode* reverseKGroup(ListNode* head, int k) {
        ListNode* cur = head;
        int len = 0; 
        while (cur)
        {
            len++;
            cur = cur->next;
        }
        len /= k; // 可以翻转len组
        cur = head;
        ListNode* newhead = new ListNode;
        ListNode* tail = newhead;
        for (int i = 0; i < len; i++)
        {
            ListNode* tmp = cur; // 标记翻转后的尾节点
            for (int j = 0; j < k; j++)
            {
                ListNode* next = cur->next;
                cur->next = tail->next;
                tail->next = cur;
                cur = next;
            }
            tail = tmp; // 更新尾节点
        }
        tail->next = cur; // 将可能剩余的拼接到后面
        tail = newhead->next;
        delete newhead;
        return tail;
    }
};
    